package com.Jin.controller;

import com.Jin.exception.SqlException;
import com.Jin.pojo.CarPlate;
import com.Jin.pojo.User;
import com.Jin.service.CarPlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarPlateControl {

    @Autowired
    CarPlateService carPlateService;    //车牌业务

    //查询当前账户车牌
    @GetMapping
    public Result selectPlate(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("user");
        String account = user.getAccount();
        List<CarPlate> carPlateList = carPlateService.selectUserPlate(account);

        Integer code=0;
        String msg = "";
        if (carPlateList!=null){
            carPlateList.sort(CarPlate.Comparators.ID);
            code = Code.SELECT_OK;
            msg = "查询车牌成功-all";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询车牌失败-all";
        }
        return new Result(code,msg,carPlateList);
    }

    //查询自己已拥有车牌，为了在摧毁列表展示
    @GetMapping("/getHave")
    public Result selectHave(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("user");
        String account = user.getAccount();
        List<CarPlate> carPlateList = carPlateService.selectHave(account,"登记车牌申请成功");

        Integer code=0;
        String msg = "";
        if (carPlateList!=null){
            carPlateList.sort(CarPlate.Comparators.ID);
            code = Code.SELECT_OK;
            msg = "查询车牌成功-have";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询车牌失败-have";
        }
        return new Result(code,msg,carPlateList);
    }

    //查询申请状态车牌
    @GetMapping("/getApply")
    public Result selectApplyPlate(HttpServletRequest req){
        HttpSession session = req.getSession();
        List<CarPlate> carPlateList = carPlateService.selectPlateByStatus("登记车牌申请中");

        Integer code=0;
        String msg = "";
        if (carPlateList!=null){
            carPlateList.sort(CarPlate.Comparators.ID);
            session.setAttribute("applyNum",carPlateList.size());
            code = Code.SELECT_OK;
            msg = "查询车牌成功-apply";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询车牌失败-apply";
        }
        msg = String.valueOf(carPlateList.size());
        return new Result(code,msg,carPlateList);
    }

    //查询摧毁状态车牌
    @GetMapping("/getDestroy")
    public Result selectDestroyPlate(HttpServletRequest req){
        HttpSession session = req.getSession();
        List<CarPlate> carPlateList = carPlateService.selectPlateByStatus("销毁车牌申请中");

        Integer code=0;
        String msg = "";
        if (carPlateList!=null){
            carPlateList.sort(CarPlate.Comparators.ID);
            session.setAttribute("destroyNum",carPlateList.size());
            code = Code.SELECT_OK;
            msg = "查询车牌成功-destroy";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询车牌失败-destroy";
        }
        msg = String.valueOf(carPlateList.size());
        return new Result(code,msg,carPlateList);
    }

    //查询所有车牌
    @GetMapping("/getAll")
    public Result selectAllPlate(){
        List<CarPlate> carPlateList = carPlateService.selectAllPlate();

        Integer code=0;
        String msg = "";
        if (carPlateList!=null){
            carPlateList.sort(CarPlate.Comparators.ID);
            code = Code.SELECT_OK;
            msg = "查询所有车牌成功";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询所有车牌失败";
        }
        return new Result(code,msg,carPlateList);
    }

    //查询车牌根据id
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id){
        CarPlate carPlate = carPlateService.selectById(id);

        Integer code=0;
        String msg = "";
        if (carPlate!=null){
            code = Code.SELECT_OK;
            msg = "查询车牌id成功";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询车牌id失败";
        }
        return new Result(code,msg,carPlate);
    }

    //更改车牌信息
    @PutMapping
    public Result updatePlate(@RequestBody CarPlate carPlate){
        boolean flag = carPlateService.updatePlate(carPlate);
        Integer code=0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "更新车牌信息成功";
            System.out.println("updateCar: "+carPlate);
        }else{
            code = Code.UPDATE_ERR;
            msg = "更新车牌信息失败";
        }
        return new Result(code,msg,flag);
    }

    //删除车牌信息
    @DeleteMapping("/{id}")
    public Result deletePlate(@PathVariable("id") int id){
        CarPlate carPlate = carPlateService.selectById(id);
        boolean flag = carPlateService.deletePlate(id);

        Integer code=0;
        String msg = "";
        if (flag){
            code = Code.DELETE_OK;
            msg = "查询车牌id成功";
            System.out.println("deletePlate: "+carPlate);
        }else{
            code = Code.DELETE_ERR;
            msg = "查询车牌id失败";
        }
        return new Result(code,msg,flag);
    }

    //提交车牌申请
    @PostMapping("/apply")
    public Result applyPlate(@RequestBody CarPlate carPlate,HttpServletRequest req) throws SQLIntegrityConstraintViolationException {
        User user = (User) req.getSession().getAttribute("user");
        String account = user.getAccount();
        String owner = user.getName();
        carPlate.setOwner(owner);
        carPlate.setAccount(account);

        boolean flag = false;
        Integer code = 0;
        String msg = "";
        try{
            flag = carPlateService.addPlate(carPlate);
            if (flag){
                code = Code.UPDATE_OK;
                msg = "添加申请成功";
                System.out.println("apply: "+carPlate);
            }else{
                code = Code.UPDATE_ERR;
                msg = "添加申请失败";
            }
        } catch (Exception e) {
            throw new SQLIntegrityConstraintViolationException("车牌号已存在",e);
        }

        return new Result(code,msg,carPlate);
    }

    //提交销毁申请
    @PutMapping("/applyDestroy")
    public Result applyDestroy(@RequestBody CarPlate carPlate){
        carPlate.setStatus("销毁车牌申请中");      //status改为销毁车牌申请中
        boolean flag = carPlateService.updatePlate(carPlate);

        Integer code = 0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "添加销毁申请成功";
            System.out.println("applyDestory: "+carPlate);
        }else{
            code = Code.UPDATE_ERR;
            msg = "添加销毁申请失败";
        }
        return new Result(code,msg,carPlate);
    }

    //同意apply
    @PutMapping("/acceptApply/{id}")
    public Result AcceptApply(@PathVariable("id")int id){
        boolean flag = carPlateService.handleApply(id,"登记车牌申请成功");

        Integer code = 0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "同意车牌申请成功";
            System.out.println("acceptApply: "+id);
        }else{
            code = Code.UPDATE_ERR;
            msg = "同意车牌申请失败";
        }
        return new Result(code,msg,flag);
    }

    //拒绝apply
    @PutMapping("/refuseApply/{id}")
    public Result RefuseApply(@PathVariable("id")int id){
        boolean flag = carPlateService.handleApply(id,"登记车牌申请失败");

        Integer code = 0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "拒绝车牌申请成功";
            System.out.println("refuseApply: "+id);
        }else{
            code = Code.UPDATE_ERR;
            msg = "拒绝车牌申请失败";
        }
        return new Result(code,msg,flag);
    }

    //同意destroy
    @PutMapping("/acceptDestroy/{id}")
    public Result AcceptDestroy(@PathVariable("id")int id){
        boolean flag = carPlateService.handleApply(id,"销毁车牌申请成功");

        Integer code = 0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "同意销毁车牌申请成功";
            System.out.println("acceptDestroy: "+id);
        }else{
            code = Code.UPDATE_ERR;
            msg = "同意销毁车牌申请失败";
        }
        return new Result(code,msg,flag);
    }

    //拒绝apply
    @PutMapping("/refuseDestroy/{id}")
    public Result RefuseDestroy(@PathVariable("id")int id){
        boolean flag = carPlateService.handleApply(id,"销毁车牌申请失败");

        Integer code = 0;
        String msg = "";
        if (flag){
            code = Code.UPDATE_OK;
            msg = "拒绝销毁车牌申请成功";
            System.out.println("refuseDestroy: "+id);
        }else{
            code = Code.UPDATE_ERR;
            msg = "拒绝销毁车牌申请失败";
        }
        return new Result(code,msg,flag);
    }



}
